package net.mymilkedeek.ludum.tinythumb;

import net.mymilkedeek.ludum.tinythumb.util.Constants;
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO: Write Documentation
 *
 * @author Michael Demey
 */
public class CollisionMap {
    private TiledMap tiledMap;
    private List<Collision> collisions;
    private int[] square = {0,0,31,0,31,31,0,31};
    private Map<Coord, String> characters;
    
    private List<EventCollision> events;

    public List<EventCollision> getEvents() {
        return events;
    }

    public CollisionMap() {
    }

    public void setTiledMap(TiledMap tiledMap) {
        this.tiledMap = tiledMap;
        collisions = new ArrayList<Collision>();        
        characters = new HashMap<Coord, String>();

        events = new ArrayList<EventCollision>();
                
        int index = tiledMap.getLayerIndex("collision");
        int indexObject = tiledMap.getLayerIndex("character");
        int tileX = tiledMap.getWidth();
        int tileY = tiledMap.getHeight();
        int tileId;
        int characterId;
        int objectLayer = tiledMap.getLayerIndex("objectlayer");

        int objectGroupcount = tiledMap.getObjectGroupCount();
        int countedGroups = 0;
        int count = 0;

        boolean breakoutofeverything = false;
        /*
        // DO NOT
        // DO NOT TOUCH THIS
        // EVER
        while ( countedGroups <= objectGroupcount && !breakoutofeverything ) {
            int objectCount = tiledMap.getObjectCount(count);
            if ( objectCount >  0) {
                int countedObjects = 0;
                int objectIndex = 0;

                while ( countedObjects <= objectCount && !breakoutofeverything) {

                    String name = tiledMap.getObjectName(count, objectIndex);

                    if ( name != null ) {

                        Log.debug(name);

                        // SO HERE IS YOUR OBJECT NAME WANKER

                        int objectX = tiledMap.getObjectX(count, objectIndex);
                        int objectY = tiledMap.getObjectY(count, objectIndex);
                        
                        Log.debug(objectX + " " + objectY);

                        Coord coord = new Coord(objectX / 32, objectY / 32);
                        
                        Log.debug(coord.getX() + " " + coord.getY());

                        // HERE HERE COME GET EM
                        characters.put(coord, name);

                        countedObjects++;
                        objectIndex++;
                    }
                    
                    if ( countedObjects == objectCount ) {
                        Log.debug("fkeying out of here");
                        break;
                    }
                }
                
                Log.debug("stuck here?");

                countedGroups++;
            }
            count++;
            
            if ( countedGroups == objectGroupcount ) {
                break;
            }
        }
        */
        
        Log.debug("done looping");

        for ( int i = 0; i < tileX; i++) {
            for ( int j = 0; j < tileY; j++ ) {
                tileId = tiledMap.getTileId(i, j, index);
                //characterId = tiledMap.getTileId(i, j, indexObject);
                if ( tileId == 401 ) {
                    collisions.add(new Collision(i * Constants.tile_size, j * Constants.tile_size, square));
                }
                
                if ( tileId == 402 ) {
                    events.add(new EventCollision(i * Constants.tile_size, j * Constants.tile_size, square));
                }
            }
        }
    }

    public TiledMap getTiledMap() {
        return tiledMap;
    }

    public List<Collision> getCollisions() {
        return collisions;
    }

    public Map<Coord, String> getCharacters() {
        return characters;
    }
}